#include "WS_Matrix.h"
uint8_t RGB_Data[3] = {30,30,30}; 
uint8_t RGB_Data2[192][3] = {               
  // English: Please note that the brightness of the lamp bead should not be too high, which can easily cause the temperature of the board to rise rapidly, thus damaging the board !!!
  // Chinese: 请注意，灯珠亮度不要太高，容易导致板子温度急速上升，从而损坏板子!!!
  // {64, 1, 1},{63, 2, 2},{62, 3, 3},{61, 4, 4},{60, 5, 5},{59, 6, 6},{58, 7, 7},{57, 8, 8},
  // {56, 9, 9},{55, 10, 10},{54, 11, 11},{53, 12, 12},{52, 13, 13},{51, 14, 14},{50, 15, 15},{49, 16, 16},
  // {48, 17, 17},{47, 18, 18},{46, 19, 19},{45, 20, 20},{44, 21, 21},{43, 22, 22},{42, 23, 23},{41, 24, 24},
  // {40, 25, 25},{39, 26, 26},{38, 27, 27},{37, 28, 28},{36, 29, 29},{35, 30, 30},{34, 31, 31},{33, 32, 32},
  // {32, 32, 33},{31, 31, 34},{30, 30, 35},{29, 29, 36},{28, 28, 37},{27, 27, 38},{26, 26, 39},{25, 25, 40},
  // {24, 24, 41},{23, 23, 42},{22, 22, 43},{21, 21, 44},{20, 20, 45},{19, 19, 46},{18, 18, 47},{17, 17, 48},
  // {16, 16, 49},{15, 15, 50},{14, 14, 51},{13, 13, 52},{12, 12, 53},{11, 11, 54},{10, 10, 55},{9, 9, 56},
  // {8, 8, 57},{7, 7, 58},{6, 6, 59},{5, 5, 60},{4, 4, 61},{3, 3, 62},{2, 2, 63},{1, 1, 64},
  
  // {1, 1, 64},{2, 2, 63},{3, 3, 62},{4, 4, 61},{5, 5, 60},{6, 6, 59},{7, 7, 58},{8, 8, 57},
  // {9, 9, 56},{10, 10, 55},{11, 11, 54},{12, 12, 53},{13, 13, 52},{14, 14, 51},{15, 15, 50},{16, 16, 49},
  // {17, 17, 48},{18, 18, 47},{19, 19, 46},{20, 20, 45},{21, 21, 44},{22, 22, 43},{23, 23, 42},{24, 24, 41},
  // {25, 25, 40},{26, 26, 39},{27, 27, 38},{28, 28, 37},{29, 29, 36},{30, 30, 35},{31, 31, 34},{32, 32, 33},
  // {33, 32, 32},{34, 31, 31},{35, 30, 30},{36, 29, 29},{37, 28, 28},{38, 27, 27},{39, 26, 26},{40, 25, 25},
  // {41, 24, 24},{42, 23, 23},{43, 22, 22},{44, 21, 21},{45, 20, 20},{46, 19, 19},{47, 18, 18},{48, 17, 17},
  // {49, 16, 16},{50, 15, 15},{51, 14, 14},{52, 13, 13},{53, 12, 12},{54, 11, 11},{55, 10, 10},{56, 9, 9},
  // {57, 8, 8},{58, 7, 7},{59, 6, 6},{60, 5, 5},{61, 4, 4},{62, 3, 3},{63, 2, 2},{64, 1, 1}

  {64, 1, 0}, {63, 2, 0}, {62, 3, 0}, {61, 4, 0}, {60, 5, 0}, {59, 6, 0}, {58, 7, 0}, {57, 8, 0},
  {56, 9, 0}, {55, 10, 0}, {54, 11, 0}, {53, 12, 0}, {52, 13, 0}, {51, 14, 0}, {50, 15, 0}, {49, 16, 0},
  {48, 17, 0}, {47, 18, 0}, {46, 19, 0}, {45, 20, 0}, {44, 21, 0}, {43, 22, 0}, {42, 23, 0}, {41, 24, 0},
  {40, 25, 0}, {39, 26, 0}, {38, 27, 0}, {37, 28, 0}, {36, 29, 0}, {35, 30, 0}, {34, 31, 0}, {33, 32, 0},
  {32, 33, 0}, {31, 34, 0}, {30, 35, 0}, {29, 36, 0}, {28, 37, 0}, {27, 38, 0}, {26, 39, 0}, {25, 40, 0},
  {24, 41, 0}, {23, 42, 0}, {22, 43, 0}, {21, 44, 0}, {20, 45, 0}, {19, 46, 0}, {18, 47, 0}, {17, 48, 0},
  {16, 49, 0}, {15, 50, 0}, {14, 51, 0}, {13, 52, 0}, {12, 53, 0}, {11, 54, 0}, {10, 55, 0}, {9, 56, 0},
  {8, 57, 0}, {7, 58, 0}, {6, 59, 0}, {5, 60, 0}, {4, 61, 0}, {3, 62, 0}, {2, 63, 0}, {1, 64, 0},

  {0, 64, 1}, {0, 63, 2}, {0, 62, 3}, {0, 61, 4}, {0, 60, 5}, {0, 59, 6}, {0, 58, 7}, {0, 57, 8},
  {0, 56, 9}, {0, 55, 10}, {0, 54, 11}, {0, 53, 12}, {0, 52, 13}, {0, 51, 14}, {0, 50, 15}, {0, 49, 16},
  {0, 48, 17}, {0, 47, 18}, {0, 46, 19}, {0, 45, 20}, {0, 44, 21}, {0, 43, 22}, {0, 42, 23}, {0, 41, 24},
  {0, 40, 25}, {0, 39, 26}, {0, 38, 27}, {0, 37, 28}, {0, 36, 29}, {0, 35, 30}, {0, 34, 31}, {0, 33, 32},
  {0, 32, 33}, {0, 31, 34}, {0, 30, 35}, {0, 29, 36}, {0, 28, 37}, {0, 27, 38}, {0, 26, 39}, {0, 25, 40},
  {0, 24, 41}, {0, 23, 42}, {0, 22, 43}, {0, 21, 44}, {0, 20, 45}, {0, 19, 46}, {0, 18, 47}, {0, 17, 48},
  {0, 16, 49}, {0, 15, 50}, {0, 14, 51}, {0, 13, 52}, {0, 12, 53}, {0, 11, 54}, {0, 10, 55}, {0, 9, 56},
  {0, 8, 57}, {0, 7, 58}, {0, 6, 59}, {0, 5, 60}, {0, 4, 61}, {0, 3, 62}, {0, 2, 63}, {0, 1, 64},

  {1, 0, 64}, {2, 0, 63}, {3, 0, 62}, {4, 0, 61}, {5, 0, 60}, {6, 0, 59}, {7, 0, 58}, {8, 0, 57},
  {9, 0, 56}, {10, 0, 55}, {11, 0, 54}, {12, 0, 53}, {13, 0, 52}, {14, 0, 51}, {15, 0, 50}, {16, 0, 49},
  {17, 0, 48}, {18, 0, 47}, {19, 0, 46}, {20, 0, 45}, {21, 0, 44}, {22, 0, 43}, {23, 0, 42}, {24, 0, 41},
  {25, 0, 40}, {26, 0, 39}, {27, 0, 38}, {28, 0, 37}, {29, 0, 36}, {30, 0, 35}, {31, 0, 34}, {32, 0, 33},
  {33, 0, 32}, {34, 0, 31}, {35, 0, 30}, {36, 0, 29}, {37, 0, 28}, {38, 0, 27}, {39, 0, 26}, {40, 0, 25},
  {41, 0, 24}, {42, 0, 23}, {43, 0, 22}, {44, 0, 21}, {45, 0, 20}, {46, 0, 19}, {47, 0, 18}, {48, 0, 17},
  {49, 0, 16}, {50, 0, 15}, {51, 0, 14}, {52, 0, 13}, {53, 0, 12}, {54, 0, 11}, {55, 0, 10}, {56, 0, 9},
  {57, 0, 8}, {58, 0, 7}, {59, 0, 6}, {60, 0, 5}, {61, 0, 4}, {62, 0, 3}, {63, 0, 2}, {64, 0, 1}
};
uint8_t RGB_Data1[64][3] = {};
uint8_t Matrix_Data[8][8];  
Adafruit_NeoPixel pixels(RGB_COUNT, RGB_Control_PIN, NEO_RGB + NEO_KHZ800); 

void RGB_Matrix1(int x) {
  for (int row = 0; row < Matrix_Row; row++) {
    for (int col = 0; col < Matrix_Col; col++) {
      if(x<16)
      {
        RGB_Data1[row*8+col][0] = RGB_Data2[row*8+col+x*8][0];
        RGB_Data1[row*8+col][1] = RGB_Data2[row*8+col+x*8][1];
        RGB_Data1[row*8+col][2] = RGB_Data2[row*8+col+x*8][2];
      }
      else
      {
        if(x+row<24)
        {
          RGB_Data1[row*8+col][0] = RGB_Data2[row*8+col+x*8][0];
          RGB_Data1[row*8+col][1] = RGB_Data2[row*8+col+x*8][1];
          RGB_Data1[row*8+col][2] = RGB_Data2[row*8+col+x*8][2];
        }
        else
        {
          RGB_Data1[row*8+col][0] = RGB_Data2[(x+row-24)*8+col][0];
          RGB_Data1[row*8+col][1] = RGB_Data2[(x+row-24)*8+col][1];
          RGB_Data1[row*8+col][2] = RGB_Data2[(x+row-24)*8+col][2];
        }
      }
    }
  }
  for (int row = 0; row < Matrix_Row; row++) {
    for (int col = 0; col < Matrix_Col; col++) {
    // int hue = ((i * 256 / RGB_COUNT) % 256)*2;
    // pixels.setPixelColor(i, pixels.ColorHSV(hue, 255, 10)); 
      if(Matrix_Data[row][col] == 1)      
      {
        pixels.setPixelColor(row*8+col, pixels.Color(RGB_Data1[row*8+col][0], RGB_Data1[row*8+col][1], RGB_Data1[row*8+col][2]));   
      }
      else
      {
        pixels.setPixelColor(row*8+col, pixels.Color(0, 0, 0)); 
      }
    }
  }
  pixels.show();
}

void RGB_Matrix1_Off(void) {
  for (int row = 0; row < Matrix_Row; row++) {
    for (int col = 0; col < Matrix_Col; col++) {
        pixels.setPixelColor(row*8+col, pixels.Color(0, 0, 0)); 
    }
  }
  pixels.show();
}

// 显示绿色
void RGB_Matrix1_Green(void) {
  for (int row = 0; row < Matrix_Row; row++) {
    for (int col = 0; col < Matrix_Col; col++) {
        // 设置绿色 (R=0, G=255, B=0)
        pixels.setPixelColor(row*8+col, pixels.Color(0, 255, 0)); 
    }
  }
  pixels.show();
}

// 显示红色
void RGB_Matrix1_Red(void) {
  for (int row = 0; row < Matrix_Row; row++) {
    for (int col = 0; col < Matrix_Col; col++) {
        // 设置红色 (R=255, G=0, B=0)
        pixels.setPixelColor(row*8+col, pixels.Color(255, 0, 0)); 
    }
  }
  pixels.show();
}

// 显示蓝色
void RGB_Matrix1_Blue(void) {
  for (int row = 0; row < Matrix_Row; row++) {
    for (int col = 0; col < Matrix_Col; col++) {
        // 设置蓝色 (R=0, G=0, B=255)
        pixels.setPixelColor(row*8+col, pixels.Color(0, 0, 255)); 
    }
  }
  pixels.show();
}

void RGB_Matrix() {
  for (int row = 0; row < Matrix_Row; row++) {
    for (int col = 0; col < Matrix_Col; col++) {
      if(Matrix_Data[row][col] == 1)      
      {
        pixels.setPixelColor(row*8+col, pixels.Color(RGB_Data[0], RGB_Data[1], RGB_Data[2]));   
      }
      else
      {
        pixels.setPixelColor(row*8+col, pixels.Color(0, 0, 0)); 
      }
    }
  }
  pixels.show();
}

uint8_t x=4,y=4;
void Game(uint8_t X_EN,uint8_t Y_EN) 
{
  Matrix_Data[x][y] = 0;
  if(X_EN && Y_EN){
    if(X_EN == 1)
      x=x+1;
    else
      x=x-1;
    if(Y_EN == 1)
      y=y+1;
    else
      y=y-1;
  }
  else if(X_EN){
    if(X_EN == 1)
      x=x+1;
    else
      x=x-1;
  }
  else if(Y_EN){
    if(Y_EN == 1)
      y=y+1;
    else
      y=y-1;
  }
  if(x < 0) x = 0;
  if(x == 8) x = 7;
  if(x > 8) x = 0;
  if(y < 0) y = 0;
  if(y == 8) y = 7;
  if(y > 8) y = 0;
  printf("%d\r\n",y);
  Matrix_Data[x][y]=1;
  RGB_Matrix();
}

void Matrix_Init() {
  pixels.begin();
  
  // English: Please note that the brightness of the lamp bead should not be too high, which can easily cause the temperature of the board to rise rapidly, thus damaging the board !!!
  // Chinese: 请注意，灯珠亮度不要太高，容易导致板子温度急速上升，从而损坏板子!!!
  pixels.setBrightness(20);
  memset(Matrix_Data, 1, sizeof(Matrix_Data)); 
}